using System;
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class CutsceneUtils : MonoBehaviour
{
	public enum DRH_MixerChannels
	{
		Effect,
		Music,
		Dialogue,
		Master
	}

	public static CutsceneUtils instance;

	public INT_Chat UtilChatbox;

	public AudioSource UtilSource;

	[SerializeField]
	private AudioMixerGroup MixerGroup_Effects;

	[SerializeField]
	private AudioMixerGroup MixerGroup_Music;

	[SerializeField]
	private AudioMixerGroup MixerGroup_Dialogue;

	[SerializeField]
	private AudioMixerGroup MixerGroup_Master;

	private void Awake()
	{
		instance = this;
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	public static void RotateCharacterToDirection(Animator targetAnimator, string X_floatName, string Y_floatname, Vector2 Direction)
	{
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		if (((Behaviour)targetAnimator).isActiveAndEnabled)
		{
			targetAnimator.SetFloat(X_floatName, Direction.x);
			targetAnimator.SetFloat(Y_floatname, Direction.y);
		}
		else
		{
			Debug.Log((object)"Unable to rotate Character! | targetAnimator isn't active and or enabled");
		}
	}

	public static void RotateCharacterTowardsPosition(Animator targetAnimator, string X_floatName, string Y_floatname, Vector2 targetPosition)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		if (((Behaviour)targetAnimator).isActiveAndEnabled)
		{
			Vector2 val = Vector2.op_Implicit(((Component)targetAnimator).transform.position);
			Vector2 val2 = targetPosition - val;
			((Vector2)(ref val2)).Normalize();
			targetAnimator.SetFloat(X_floatName, val2.x);
			targetAnimator.SetFloat(Y_floatname, val2.y);
		}
		else
		{
			Debug.Log((object)"Unable to rotate towards position! | targetAnimator isn't active and or enabled");
		}
	}

	public static void ShakeTransform(Transform target, float multiplier = 0.25f, float duration = 2f)
	{
		if (((Component)target).gameObject.activeSelf)
		{
			((MonoBehaviour)instance).StartCoroutine(instance.ShakeTarget(target, multiplier, duration));
		}
		else
		{
			Debug.Log((object)"Unable to shake transform! | transform gameobject not enabled");
		}
	}

	public static void PlaySound(AudioClip clip, DRH_MixerChannels TargetMixerGroup = DRH_MixerChannels.Effect, float Volume = 1f, float Pitch = 1f)
	{
		if ((Object)(object)clip != (Object)null)
		{
			switch (TargetMixerGroup)
			{
			case DRH_MixerChannels.Effect:
				instance.UtilSource.outputAudioMixerGroup = instance.MixerGroup_Effects;
				break;
			case DRH_MixerChannels.Music:
				instance.UtilSource.outputAudioMixerGroup = instance.MixerGroup_Music;
				break;
			case DRH_MixerChannels.Dialogue:
				instance.UtilSource.outputAudioMixerGroup = instance.MixerGroup_Dialogue;
				break;
			case DRH_MixerChannels.Master:
				instance.UtilSource.outputAudioMixerGroup = instance.MixerGroup_Master;
				break;
			}
			instance.UtilSource.pitch = Pitch;
			instance.UtilSource.PlayOneShot(clip, Volume);
		}
		else
		{
			Debug.Log((object)"Unable to play sound! | clip is missing or null");
		}
	}

	public IEnumerator ShakeTarget(Transform target, float multiplier = 1f, float duration = 1f)
	{
		if ((Object)(object)target != (Object)null)
		{
			Vector3 originalPosition = target.position;
			float elapsedTime = 0f;
			while (multiplier > 0f && !((Object)(object)target == (Object)null))
			{
				float num = Random.Range(-1f, 1f) * multiplier;
				target.position = new Vector3(originalPosition.x + num, originalPosition.y, originalPosition.z);
				elapsedTime += Time.fixedDeltaTime;
				multiplier -= Time.fixedDeltaTime * (1f / duration);
				yield return null;
			}
			if ((Object)(object)target != (Object)null)
			{
				target.position = originalPosition;
			}
		}
	}

	public static void RunFreshChat(CHATBOXTEXT text, int index, bool ForcePosition, bool OnBottom)
	{
		if ((Object)(object)text != (Object)null)
		{
			instance.UtilChatbox.FirstTextPlayed = false;
			instance.UtilChatbox.CurrentIndex = index;
			instance.UtilChatbox.FinishedText = false;
			instance.UtilChatbox.Text = text;
			if (ForcePosition)
			{
				instance.UtilChatbox.ManualTextboxPosition = true;
				instance.UtilChatbox.OnBottom = OnBottom;
			}
			instance.UtilChatbox.RUN();
		}
		else
		{
			Debug.Log((object)"Unable to run fresh chat! | CHATBOXTEXT is null");
		}
	}

	public static void FadeInSprite(SpriteRenderer spriteRenderer, float fadeSpeed = 1f, float targetAlpha = 1f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeInSpriteTimed(spriteRenderer, fadeSpeed, targetAlpha));
	}

	public static void FadeOutSprite(SpriteRenderer spriteRenderer, float fadeSpeed = 1f, float targetAlpha = 0f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeOutSpriteTimed(spriteRenderer, fadeSpeed, targetAlpha));
	}

	public static void FadeSpriteToColor(SpriteRenderer spriteRenderer, Color targetColor, float fadeSpeed = 1f)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		((MonoBehaviour)instance).StartCoroutine(FadeSpriteToColorTimed(spriteRenderer, targetColor, fadeSpeed));
	}

	private static IEnumerator FadeInSpriteTimed(SpriteRenderer spriteRenderer, float fadeSpeed, float targetAlpha = 1f)
	{
		if ((Object)(object)spriteRenderer != (Object)null)
		{
			Color spriteColor = spriteRenderer.color;
			while (spriteColor.a < targetAlpha && (Object)(object)spriteRenderer != (Object)null)
			{
				spriteColor.a += fadeSpeed * Time.deltaTime;
				spriteColor.a = Mathf.Clamp(spriteColor.a, 0f, targetAlpha);
				spriteRenderer.color = spriteColor;
				yield return null;
			}
		}
		else
		{
			Debug.LogWarning((object)"FadeInSprite failed! spriteRenderer is null");
		}
	}

	private static IEnumerator FadeOutSpriteTimed(SpriteRenderer spriteRenderer, float fadeSpeed, float targetAlpha = 0f)
	{
		if ((Object)(object)spriteRenderer != (Object)null)
		{
			Color spriteColor = spriteRenderer.color;
			while (spriteColor.a > targetAlpha)
			{
				spriteColor.a -= fadeSpeed * Time.deltaTime;
				spriteColor.a = Mathf.Clamp(spriteColor.a, targetAlpha, 1f);
				spriteRenderer.color = spriteColor;
				yield return null;
			}
		}
		else
		{
			Debug.LogWarning((object)"FadeOutSprite failed! spriteRenderer is null");
		}
	}

	private static IEnumerator FadeSpriteToColorTimed(SpriteRenderer spriteRenderer, Color targetColor, float fadeSpeed)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		Color currentColor = spriteRenderer.color;
		while (currentColor != targetColor)
		{
			currentColor = (spriteRenderer.color = Color.Lerp(currentColor, targetColor, fadeSpeed * Time.deltaTime));
			if (Vector4.Distance(Color.op_Implicit(currentColor), Color.op_Implicit(targetColor)) < 0.01f)
			{
				spriteRenderer.color = targetColor;
				break;
			}
			yield return null;
		}
	}

	public static void FadeInText(TextMeshProUGUI textMeshUI, float fadeSpeed = 1f, float targetAlpha = 1f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeInTextTimed(textMeshUI, fadeSpeed, targetAlpha));
	}

	public static void FadeOutText(TextMeshProUGUI textMeshUI, float fadeSpeed = 1f, float targetAlpha = 0f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeOutTextTimed(textMeshUI, fadeSpeed, targetAlpha));
	}

	private static IEnumerator FadeInTextTimed(TextMeshProUGUI textMeshUI, float fadeSpeed, float targetAlpha = 1f)
	{
		Color textColor = ((Graphic)textMeshUI).color;
		while (textColor.a < targetAlpha)
		{
			textColor.a += fadeSpeed * Time.deltaTime;
			textColor.a = Mathf.Clamp(textColor.a, 0f, targetAlpha);
			((Graphic)textMeshUI).color = textColor;
			yield return null;
		}
	}

	private static IEnumerator FadeOutTextTimed(TextMeshProUGUI textMeshUI, float fadeSpeed, float targetAlpha = 0f)
	{
		Color textColor = ((Graphic)textMeshUI).color;
		while (textColor.a > targetAlpha)
		{
			textColor.a -= fadeSpeed * Time.deltaTime;
			textColor.a = Mathf.Clamp(textColor.a, targetAlpha, 1f);
			((Graphic)textMeshUI).color = textColor;
			yield return null;
		}
	}

	public static void FadeInText3D(TextMeshPro textMesh, float fadeSpeed = 1f, float targetAlpha = 1f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeInText3DTimed(textMesh, fadeSpeed, targetAlpha));
	}

	public static void FadeOutText3D(TextMeshPro textMesh, float fadeSpeed = 1f, float targetAlpha = 0f)
	{
		((MonoBehaviour)instance).StartCoroutine(FadeOutText3DTimed(textMesh, fadeSpeed, targetAlpha));
	}

	private static IEnumerator FadeInText3DTimed(TextMeshPro textMesh, float fadeSpeed, float targetAlpha = 1f)
	{
		Color textColor = ((Graphic)textMesh).color;
		while (textColor.a < targetAlpha)
		{
			textColor.a += fadeSpeed * Time.deltaTime;
			textColor.a = Mathf.Clamp(textColor.a, 0f, targetAlpha);
			((Graphic)textMesh).color = textColor;
			yield return null;
		}
	}

	private static IEnumerator FadeOutText3DTimed(TextMeshPro textMesh, float fadeSpeed, float targetAlpha = 0f)
	{
		Color textColor = ((Graphic)textMesh).color;
		while (textColor.a > targetAlpha)
		{
			textColor.a -= fadeSpeed * Time.deltaTime;
			textColor.a = Mathf.Clamp(textColor.a, targetAlpha, 1f);
			((Graphic)textMesh).color = textColor;
			yield return null;
		}
	}

	public static void MoveTransformOnArc(Transform target, Vector3 endPoint, float height, float duration, bool rotateAlongArc = false)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		((MonoBehaviour)instance).StartCoroutine(ArcMotionRoutine(target, endPoint, height, duration, rotateAlongArc));
	}

	private static IEnumerator ArcMotionRoutine(Transform target, Vector3 endPoint, float height, float duration, bool rotateAlongArc)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		Vector3 startPoint = target.position;
		Vector3 previousPosition = startPoint;
		float elapsedTime = 0f;
		Vector3 val4;
		while (elapsedTime < duration)
		{
			elapsedTime += Time.deltaTime;
			float num = Mathf.Clamp01(elapsedTime / duration);
			Vector3 val = Vector3.Lerp(startPoint, endPoint, num);
			float num2 = Mathf.Sin(num * MathF.PI) * height;
			Vector3 val2 = (target.position = val + new Vector3(0f, num2, 0f));
			if (rotateAlongArc && num > 0f)
			{
				val4 = val2 - previousPosition;
				Vector3 normalized = ((Vector3)(ref val4)).normalized;
				float num3 = Mathf.Atan2(normalized.y, normalized.x) * 57.29578f;
				target.rotation = Quaternion.Euler(0f, 0f, num3);
			}
			previousPosition = val2;
			yield return null;
		}
		target.position = endPoint;
		if (rotateAlongArc)
		{
			val4 = endPoint - startPoint;
			Vector3 normalized2 = ((Vector3)(ref val4)).normalized;
			float num4 = Mathf.Atan2(normalized2.y, normalized2.x) * 57.29578f;
			target.rotation = Quaternion.Euler(0f, 0f, num4);
		}
	}

	public static void MoveTransformLinear(Transform target, Vector3 endPoint, float duration)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)instance != (Object)null)
		{
			((MonoBehaviour)instance).StartCoroutine(MoveTransformLinearRoutine(target, endPoint, duration));
		}
	}

	public static IEnumerator MoveTransformLinearRoutine(Transform target, Vector3 endPoint, float duration)
	{
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)target == (Object)null)
		{
			Debug.LogError((object)"Target Transform cannot be null.");
			yield break;
		}
		Vector3 startPosition = target.position;
		float elapsedTime = 0f;
		while (elapsedTime < duration)
		{
			elapsedTime += Time.deltaTime;
			float num = Mathf.Clamp01(elapsedTime / duration);
			target.position = Vector3.Lerp(startPosition, endPoint, num);
			yield return null;
		}
		target.position = endPoint;
	}
}
